<!DOCTYPE html>
<html>
<!---需要技术支持联系QQ：44312658--->
<head>
	<meta charset="utf-8">
	<title>注册界面</title>
	<link rel="stylesheet" href="css/reset.css"/>
	<link rel="stylesheet" href="css/common.css"/>
	<link rel="stylesheet" href="css/font-awesome.min.css"/>

	<!--引入layui组件-->
	<link rel="stylesheet" href="layui/css/layui.css"/>
	<script type="text/javascript" src="layui/layui.js"></script>
</head>
<body>
<div class="wrap login_wrap">
	<div class="content">

		<div class="logo"></div>
		<div class="login_box">
			<div class="login_form">
				<div class="login_title">
					注册
				</div>
				<form action="#" method="post">
					<div class="form_text_ipt">
						<input name="identify" type="text" placeholder="手机号/邮箱" value="">
					</div>
					<div class="ececk_warning"><span>手机号/邮箱不能为空</span></div>
					<div class="form_text_ipt">
						<input name="password" type="password" placeholder="密码">
					</div>
					<div class="ececk_warning"><span>密码不能为空</span></div>
					<div class="form_text_ipt">
						<input name="repassword" type="password" placeholder="重复密码">
					</div>
					<div class="ececk_warning"><span>密码不能为空</span></div>
					<div style="text-align: center">
						<img src="commons/randomCodeImg" style="cursor:pointer;" id="randomCodeImg" title="看不清楚点击换一张" onclick="changeRandomCodeImg()" />
					</div>
					<div class="form_text_ipt">
						<input name="code" type="text" placeholder="验证码">
					</div>
					<div class="ececk_warning"><span>验证码不能为空</span></div>

					<div class="form_btn">
						<button type="button" onclick="doReg()">注册</button>
					</div>
					<div class="form_reg_btn">
						<span>已有帐号？</span><a href="login.html">马上登录</a>
					</div>
				</form>

				<div style="text-align: center">
					<span><a href="index.html">返回首页</a></span>
					<br/>
					<br/>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<!--引入bee工具类用来判断用户输入的信息格式-->
<script src="js/bee/bee.min.js"></script>
<div style="text-align:center;">
</div>
<script>

	layui.use(['layer'], function(){
		var layer = layui.layer;
	});

	let regUser = {};
	function doReg(){

		let identify = document.querySelector("[name='identify']").value;
		let password = document.querySelector("[name='password']").value;
		let confirmPas = document.querySelector("[name='repassword']").value;

		let randomCode = document.querySelector("[name='code']").value;


		if(Bee.PhoneUtils.isPhoneNum(identify)){
			console.log('是手机号码');
			regUser.mobile = identify;
		} else if(Bee.EmailUtils.isEmail(identify)){
			console.log('是电子邮箱');
			regUser.email = identify;
		}else{
			layer.msg('注册信息格式非法！');
			return;
		}
		regUser.password = password;
		regUser.randomCode = randomCode;
		console.log("注册的标识符："+identify);
		console.log("执行用户注册....");

		//密码与确认密码必须相同，而且位数不能少于6位
		if(regUser.password.length<6){
			layer.msg('密码不能小于6位！')
			return;
		}
		if(regUser.password!=confirmPas){
			layer.msg('密码与确认密码不同！')
			return;
		}

		$.ajax({
			url: "https://localhost:8443/servletdemo/users/reg",
			type: "POST",
			data:JSON.stringify(regUser),
			dataType: "text",
			success: function (resp) {
				let result = JSON.parse(resp); //字符串转成json对象
				console.log(resp);

				if (result.code === 200) {
					layer.msg(result.msg);
					//注册成功后，两秒钟自动跳转到登录页面。
					window.setTimeout("javascript:window.location.href = 'login.html'",2000);

				} else {
					layer.msg(result.msg)
				}
			}
		});
	}

	function changeRandomCodeImg(){
		//document.querySelector("#randomCodeImg")
		$("#randomCodeImg").prop("src","commons/randomCodeImg?tm"+new Date().getMilliseconds())
	}
</script>
</body>
</html>
